<?php

namespace backend\controllers;

use Yii;
use frontend\models\ArtForm;
use common\models\Art\ArtSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;
use common\models\adminlog\AdminLog;
use zhongkefu\helpers\Excelwriter;
use common\widgets\file_upload\FileUpload;

/**
 * ArtController implements the CRUD actions for Art model.
 */
class ArtController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'upload' => [
                'class' => 'kucha\ueditor\UEditorAction',
                'config' => [
                    "imageUrlPrefix"  =>  Yii::$app->params['ueditor'],//图片访问路径前缀
                    "imagePathFormat" => '/upload/ariticle/ueditor/{yyyy}{mm}{dd}/{time}{rand:6}',//上传保存路径
                    "imageRoot" => Yii::getAlias("@webroot"),
                ],
            ]
        ];
    }

    /**
     * Lists all Art models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ArtSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Art model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Art model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ArtForm();

        if ($model->load(Yii::$app->request->post())) {
             $model->img = UploadedFile::getInstance($model, 'img');

            if($model->img){
                $siteRoot = Yii::$app->params['articleimg'];
                $json = Utils::uploadfile($_FILES['ArtForm']['tmp_name']['img'],$_FILES['ArtForm']['name']['img'],$siteRoot);

                $model->img = $json;
            }
            if(isset(Yii::$app->request->post()['tag'])){
                $model->optiontag = json_encode(Yii::$app->request->post()['tag']);
            }
             if($model->save()){
                $title = Yii::$app->request->post()['ArtForm']['title'];
                Adminlog::addlog(yii::$app->user->id, "添加会员文章 ".$title);
                
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                Yii::$app->session->setFlash('warning', Yii::t('app', '图片不能为空'));
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
         
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Art model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) ) {

           $model->img = UploadedFile::getInstance($model, 'img');

            if($model->img){
                $siteRoot = Yii::$app->params['articleimg'];
                $json = Utils::uploadfile($_FILES['ArtForm']['tmp_name']['img'],$_FILES['ArtForm']['name']['img'],$siteRoot);
                $model->img = $json;
            }
            //若为更新图片，则为原数据库中图片名称
            if (!$model->img) {
                $new = $this->findModel($id);
                $model->img = $new->img;
            }
            if(isset(Yii::$app->request->post()['tag'])){
                $model->optiontag = json_encode(Yii::$app->request->post()['tag']);
            }
            if($model->save()){
                $title = Yii::$app->request->post()['ArtForm']['title'];
                Adminlog::addlog(yii::$app->user->id, "更新会员文章 ".$title);

                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                return $this->render('update', [
                    'model' => $model,
                ]);
            }

        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
     }

    /**
     * Deletes an existing Art model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Art model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Art the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ArtForm::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
	public function actionUncheck(){
        $searchModel = new ArtSearch();
        $dataProvider = $searchModel->unchecksearch(Yii::$app->request->queryParams);

        return $this->render('uncheck', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    public function actionCheck(){
        $searchModel = new ArtSearch();
        $dataProvider = $searchModel->checksearch(Yii::$app->request->queryParams);

        return $this->render('check', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    public function actionWaitcheck(){
        $searchModel = new ArtSearch();
        $dataProvider = $searchModel->waitchecksearch(Yii::$app->request->queryParams);

        return $this->render('waitcheck', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
	
	
	
	
	
	
	
	    /**
     * 启用按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStart()
    {
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){
                $model = ArtForm::findOne(['id' => $id]);
                ArtForm::updateAll(['display'=>ArtForm::DIS_SUCCESS],['id' =>$model->id]);
            }
            return true;
        }
    }

    /**
     * 停用按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStop()
    {

        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $reason = zhongkefu::$app->request->post()["reason"];
            $ids = explode(',',$ids);
            foreach($ids as $id){
                $model = ArtForm::findOne(['id' => $id]);
                ArtForm::updateAll(['display'=>ArtForm::DIS_DELETED,'reason'=>$reason],['id' =>$model->id]);
            }
            return true;
        }
    }
    /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
		$artname="";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);

			foreach($ids as $val){
				$models = ArtForm::findOne(['id' => $val]);
				$artname .= $models->title.",";
			}
			$artname = substr($artname,0,-1);
			
            foreach($ids as $id){
                $model = ArtForm::findOne(['id' => $id]);
                $this->findModel($id)->delete();

            }

			Adminlog::addlog(yii::$app->user->id, '批量删除会员 '.$artname);
            return true;
        }
    }
}
